#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int gcd(int a, int b)
    {
        while (a % b != 0)
        {
            int c = a % b;
            a = b;
            b = c;
        }
        return b;
    }

    int minOperations(vector<int>& nums) {
        int once = 0, n = nums.size();
        for (auto num : nums) if (num == 1) once++;
        if (once == n) return 0;

        bool flag = false;
        int trys = 0;
        for (int i = n; i >= 0; i--, trys++)
        {
            for (int j = 0; j < i - 1; j++)
            {
                nums[j] = gcd(nums[j], nums[j + 1]);
                if (nums[j] == 1) flag = true;
            }
            if (flag) break;
        }
        if (trys > n) return -1;
        return trys + n - once;
    }
};